package app.view

import java.awt.FlowLayout

import javax.swing.ButtonGroup
import javax.swing.JCheckBox
import javax.swing.JPanel
import javax.swing.JRadioButton

import model.agent.question.QuestionInterface
import model.agent.question.QuestionSet


class LOptionsPanel extends JPanel{
	
	String name = ""

	JCheckBox totalSelect = null

	QuestionInterface question

	ArrayList controls = []

	def LOptionsPanel(QuestionInterface question){
		//super()
		this.question = question
		

		this.setLayout(new FlowLayout(java.awt.FlowLayout.LEFT))

		def questionString = question.getQuestionString()

		if(questionString.contains(QuestionSet.singleSep)){
			totalSelect = new JCheckBox()
			this.add(totalSelect)
			ButtonGroup group = new ButtonGroup();

			question.getOptions().each { String option ->
				def cb = new JRadioButton(option)
				group.add(cb)
				controls.add(cb)
				this.add(cb)
			}
		}else{
			question.getOptions().each { String option ->
				//ButtonGroup group = new ButtonGroup();
				def cb = new JCheckBox(option)

				controls.add(cb)
				this.add(cb)
			}
		}
	}

	ArrayList getSelectedOptions(){
		def result = []
		if(this.isSelected()){
			controls.each{
				if(it.isSelected()) result.add(it.getText())
			}
		}
		return result
	}

	private Boolean isSelected(){
		if(totalSelect){
			return totalSelect.isSelected()
		}
		return true
	}

}
